大家好今天繼續來示範Dialog的其他用法,在這我們開一個新的空白專案來示範,首先一樣先上程式碼。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnProgressDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Progress Dialog"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnItemDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ItemDialog"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnProgressDialog" />
</androidx.constraintlayout.widget.ConstraintLayout>
今天我們會使用到兩種Dialog,一個是progressDialg,另一個是ItemDialog,有時候我們登入某些帳號時會顯示一個登入中的Dialog,而這個Dialog就是所謂的ProgressDialog,當我們上傳或是下載檔案時也可以使用ProgressDialog來顯示進度條。
而ItemDialog在這個專案中點下這個按鈕時,會彈出一些地方的名字,而我們可以點選一個地名他則會彈出一個Toast來告知我們選到哪一個地方。
package com.example.day30_dialog1;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressDialog mProgressDialog;
private Button btnProgressDialog;
private Button btnItemDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btnProgressDialog = findViewById(R.id.btnProgressDialog);
btnItemDialog = findViewById(R.id.btnItemDialog);
btnProgressDialog.setOnClickListener(this);
btnItemDialog.setOnClickListener(this);
}
public void dismissProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.cancel();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnProgressDialog:
FiveSecond();
break;
case R.id.btnItemDialog:
showItemDialog();
break;
}
}
public void showItemDialog() {
final String[] cities = {"桃園", "新竹", "苗栗", "台中", "彰化", "雲林", "嘉義", "台南", "高雄", "屏東", "台東", "花蓮", "宜蘭", "基隆", "台北", "新北", "南投"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("ItemDialog")
.setItems(cities, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "當前選擇:" + cities[which], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
private void showProgressDialog() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("ProgressDialog...");
mProgressDialog.setCancelable(false);
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
}
private void FiveSecond() {
showProgressDialog();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
dismissProgressDialog();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
我們來看看ProgressDialog的程式碼:
private void showProgressDialog() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("ProgressDialog...");
mProgressDialog.setCancelable(false);
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
}
.setCancelable是用來設定是否可以通過點選"返回"鍵來取消Dialog
.setCanceledOnTouchOutside 是設定是否可以通過點選Dialog以外的地方來取消Dialog
我的想法是當點選ProgressDialog的按鈕時,程式碼會跳到FiveSecond的function,在裡面會做一個Thread+sleep的定時器,當五秒過後程式碼會進入dimissDialog來關閉ProgressDialog,這樣就是一個基本的ProgressDialog使用方式。
接下來我們來示範ItemDialog的程式碼
public void showItemDialog() {
final String[] cities = {"桃園", "新竹", "苗栗", "台中", "彰化", "雲林", "嘉義", "台南", "高雄", "屏東", "台東", "花蓮", "宜蘭", "基隆", "台北", "新北", "南投"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("ItemDialog")
.setItems(cities, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "當前選擇:" + cities[which], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
.setTitle設定Dialog的標題
.setItems設定要填入的選項們
.setNegativeButton設定取消鍵
這樣就是一個簡單的ItemDialog了,那我們今天就到此為止了,明天再繼續示範Dialog其他的功能。